import { Injectable } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs';
import { DepartmentManageService } from '../department-manage/department-manage.service';
import { MenuManageService } from '../menu-manage/menu-manage.service';

@Injectable({
    providedIn: 'root'
})
export class RoleManageService {

    constructor(
        private http: HttpClient,
        private deptService : DepartmentManageService,
        private menuService : MenuManageService
    ) { }

    private searchRoleUrl = '/api/roles';
    private roleListUrl = '/api/roles/all';             // 一次后去所有角色数据
    private putRoleMenuUrl = '/api/roles/menu'
    // 查询角色
    getRoles(data): Observable<any> {
        let dataJson = {
            ...data,
            page: data.page - 1
        }
        return this.http.get(this.searchRoleUrl, { params: dataJson })
    }

    getRolesAll(data?): Observable<any> {               // 用户组件使用
        return this.http.get(this.roleListUrl, {})
    }

    //新增
    apiAdd(data): Observable<any> {
        let dataJson = {
            ...data,
            depts : [{ id : data.depts }]
        }
        return this.http.post(this.searchRoleUrl, dataJson, {})
    }
    //修改
    apiEdit(data): Observable<any> {
        let dataJson = {
            ...data,
            depts : [{ id : data.depts }]
        }
        return this.http.put(this.searchRoleUrl, dataJson, {})
    }
    // 删除
    apiDelete(data): Observable<any> {
        let options = {
            headers: {
                "Content-Type": "application/json"
            },
            body: data
        }
        return this.http.delete(this.searchRoleUrl, options)
    }
    // 获取部门数据列表
    getDepartNodes(): Observable<any> {
        return this.deptService.apiList({})
    }
    //获取全部菜单
    apiGetAllMenu(): Observable<any> {
        return this.menuService.apiGetAllMenu({});
    }
    //修改角色菜单
    putRoleMenu(data): Observable<any> {
        let _dataJson = {
            ...data
        }
        return this.http.put(this.putRoleMenuUrl, _dataJson, {})
    }    
}
